package models;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;

import ch.bfh.abcm.rems.sensors.tinkerforge.TFSensorType;
import play.Logger;
import play.db.jpa.Model;

//@Entity
public class CNAverage extends Model {

    private long timestamp;
    private double plausability;
    private int nodeValue;
    private String nodeIP;
    private String measurementPointIP;
    private TFSensorType sensorType;

    protected CNAverage() {
    }

    public CNAverage(long timestamp, double plausability, int nodeValue, String nodeIP, String measurementPointIP,
            TFSensorType sensorType) {
        this.timestamp = timestamp;
        this.plausability = plausability;
        this.nodeValue = nodeValue;
        this.nodeIP = nodeIP;
        this.measurementPointIP = measurementPointIP;
        this.sensorType = sensorType;
    }

    public CNAverage(double plausability, int nodeValue, String nodeIP, String measurementPointIP,
            TFSensorType sensorType) {
        this(new Date().getTime(), plausability, nodeValue, nodeIP, measurementPointIP, sensorType);
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public int getNodeValue() {
        return nodeValue;
    }

    public void setNodeValue(int nodeValue) {
        this.nodeValue = nodeValue;
    }

    public TFSensorType getSensorType() {
        return sensorType;
    }

    public void setSensorType(TFSensorType sensorType) {
        this.sensorType = sensorType;
    }

    public String getNodeIP() {
        return nodeIP;
    }

    public void setNodeIP(String nodeIP) {
        this.nodeIP = nodeIP;
    }

    public String getMeasurementPointIP() {
        return measurementPointIP;
    }

    public void setMeasurementPointIP(String measurementPointIP) {
        this.measurementPointIP = measurementPointIP;
    }

    public double getPlausability() {
        return plausability;
    }

    public void setPlausability(double plausability) {
        this.plausability = plausability;
    }

//    public static List<CNAverage> getCNAsince(long sinceTimeStamp, TFSensorType sensorType, String nodeIP) {
//        return find("SELECT c FROM CNAverage c WHERE c.timestamp >=?1 AND c.sensorType=?2 AND c.nodeIP=?3",
//                sinceTimeStamp, sensorType, nodeIP).fetch();
//    }
//
//    public static CNAverage getLastCNAvaerage(TFSensorType sensorType) {
//        List<CNAverage> averages = find("SELECT c FROM CNAverage c WHERE c.sensorType=?1 ORDER BY c.timestamp DESC",
//                sensorType).fetch(1);
//        return averages.size() >= 1 ? averages.get(0) : null;
//    }
//
//    public static CNAverage getLastCNAvaerage(String nodeIP, String measurementPointIP) {
//        List<CNAverage> cnAverages = getLastCNAverages(nodeIP, measurementPointIP, 1);
//        if (cnAverages.size() != 1) {
//            Logger.warn("CNA not found: nodeIP: %s measurementPointIP: %s", nodeIP, measurementPointIP);
//            return null;
//        }
//        return cnAverages.get(0);
//    }
//
//    public static List<CNAverage> getLastCNAverages(String nodeIP, String measurementPointIP, int count) {
//        return find(
//                "SELECT c FROM CNAverage c WHERE c.nodeIP=?1 AND c.measurementPointIP=?2 ORDER BY c.timestamp DESC",
//                nodeIP, measurementPointIP).fetch(count);
//    }
//
//    public static List<CNAverage> getObsoleteCNA(long remainingValues) {
//        return find("SELECT c FROM CNAverage c WHERE c.id <= ((SELECT MAX(s.id) FROM CNAverage s)-?1)", remainingValues)
//                .fetch();
//    }

    @Override
    public String toString() {
        return "CNAverage [timestamp=" + timestamp + ", plausability=" + plausability + ", nodeValue=" + nodeValue
                + ", nodeIP=" + nodeIP + ", measurementPointIP=" + measurementPointIP + ", sensorType=" + sensorType
                + "]";
    }

}
